package com.tcu.order.controller;

import com.tcu.exception.SmartException;
import com.tcu.order.entity.OrderVideo;
import com.tcu.order.service.IOrderVideoService;
import com.tcu.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderVideo")
public class OrderVideoController {


    private IOrderVideoService videoService;

    @Autowired
    public void setVideoService(IOrderVideoService videoService) {
        this.videoService = videoService;
    }

    @PostMapping("add")
    public R add(@RequestBody OrderVideo orderVideo){
        if(orderVideo.getId()!=null){
            throw new SmartException(20001,"增加时，id必须为null");
        }
        boolean b = videoService.save(orderVideo);
        return b ? R.success() : R.error();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = videoService.removeById(id);
        return b ? R.success() : R.error();
    }

    @DeleteMapping("deleteByOrderId/{orderId}")
    public R deleteByOrderId(@PathVariable String orderId){
        Integer integer = videoService.deleteVideosByOrderId(orderId);
        return integer!=0 ? R.success() : R.error();
    }
    @PostMapping("update")
    public R update(@RequestBody OrderVideo orderVideo){
        if (orderVideo.getId()==null) {
            throw new SmartException(20001,"id为null，无法更新");
        }
        boolean b = videoService.updateById(orderVideo);
        return b ? R.success() : R.error();
    }

    @GetMapping("getVideosByOrderId/{orderId}")
    public R getVideosByOrderId(@PathVariable String orderId){
        List<OrderVideo> videosByOrderId = videoService.getVideosByOrderId(orderId);
        return R.success().data("videos", videosByOrderId);
    }

    @GetMapping("getSomeVideos/{orderId}/{isUser}")
    public R getSomeVideos(@PathVariable String orderId, @PathVariable Integer isUser){
        List<OrderVideo> someVideos = videoService.getSomeVideos(orderId, isUser);
        return R.success().data("videos", someVideos);
    }

}
